
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include"RTProcTextureChecker.h"


#include <math.h>

//for the memcpy
#include <cstring>

namespace RT
{
	/*Default constructor*/
	RTProcTextureChecker::RTProcTextureChecker()
	{
		m_colorChecker[0] = m_colorChecker[1] = RTColor(0, 0, 0, 1);
		m_length = 0;
		m_map = RTVector3f(0, 1, 1);
	}

	/*Constructor*/
	RTProcTextureChecker::RTProcTextureChecker(RTColor* colorChecker, F32 length, const RTVector3f& map):m_length(length),
			m_map(map)
	{
		memcpy(m_colorChecker, colorChecker, 2 * sizeof(RTColor)); 
	}

	/*Constructor*/
	RTProcTextureChecker::RTProcTextureChecker(const RTColor& color1, const RTColor& color2, F32 length, 
											 const RTVector3f& map)
											 :m_length(length), m_map(map)
	{
		m_colorChecker[0] = color1;
		m_colorChecker[1] = color2;
	}

	/*Calcualte the color*/
	RTColor RTProcTextureChecker::getColor(const RTVector3f& p)const
	{
		RTVector3f newP = m_transform * p;

		F32 u, v;
		if(m_map.x != 0 && m_map.y != 0)
		{
			u = newP.x;
			v = newP.y;
		}
		else if (m_map.x != 0 && m_map.z != 0)
		{
			u = newP.x;
			v = newP.z;
		}
		else
		{
			u = newP.y;
			v = newP.z;
		}

		//check parity of u
		I32 ratio = (I32)((I32)u / m_length);
		I32 modulo = ratio % 2;
		bool uEven = true;
		if(modulo != 0)
			uEven = false;
		if(u < 0)
			uEven = !uEven;

		//check parity of v
		ratio = (I32)((I32)v / m_length);
		modulo = ratio % 2;
		bool vEven = true;
		if(modulo != 0)
			vEven = false;
		if(v < 0)
			vEven = !vEven;


		if((vEven && uEven) || (!vEven && !uEven))
			return m_colorChecker[0];
		else
			return m_colorChecker[1];
	}
}